package org.rzp.ideaplug.statistic.setting;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.progress.util.BackgroundTaskUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class StatisticPushSettingConfig implements Configurable {

    private StatisticPushSettingComponent settingComponent;


    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "Statistic Auto Push";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        settingComponent = new StatisticPushSettingComponent();
        return settingComponent.getSettingPanel();
    }

    @Override
    public boolean isModified() {
        StatisticPushSettingState settingState = StatisticPushSettingState.getInstance();

        boolean modified = !settingComponent.getUrl().equals(settingState.url) || !settingComponent.getUser().equals(settingState.user)
                || !settingComponent.getOpen() == settingState.open || !settingComponent.getClose() == settingState.close;

        return modified;
    }

    @Override
    public void apply() throws ConfigurationException {
        StatisticPushSettingState settingState = StatisticPushSettingState.getInstance();
        settingState.user = settingComponent.getUser();
        settingState.url = settingComponent.getUrl();
        settingState.open = settingComponent.getOpen();
        settingState.close = settingComponent.getClose();
    }

    @Override
    public void reset() {
        StatisticPushSettingState settingState = StatisticPushSettingState.getInstance();
        settingComponent.settingUrl(settingState.url);
        settingComponent.settingUser(settingState.user);
        settingComponent.settingOpen(settingState.open);
        settingComponent.settingClose(settingState.close);
    }

    @Override
    public void disposeUIResources() {
        settingComponent = null;
    }
}
